//
//  YMEmoticonTools.swift
//  YMWeiBo
//
//  Created by mimi on 16/7/30.
//  Copyright © 2016年 mimi. All rights reserved.
//

import UIKit


let YMEmoticonMaxRow = 3

let YMEmoticonMaxCol = 7

let YMEmoticonMaxCount = YMEmoticonMaxRow*YMEmoticonMaxCol - 1


//定义一个单利管理数组

class YMEmoticonTools: NSObject {

    //单例对象,全局访问
    static let sharedTools:YMEmoticonTools = YMEmoticonTools()
    //emoticon bundle文件
    lazy var enmoticonBundle: NSBundle = {
       let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)
        return NSBundle(path:path!)!
    }()
    
    
    // 默认表情
    lazy var defaultEmoticons:[YMEmoticon] = {
        return self.getEmoticons("default/")
    }()
    
    // emoji表情
    lazy var emojiEmoticons:[YMEmoticon] = {
        return self.getEmoticons("emoji/")
    }()
    
    // 浪小花表情
    lazy var lxhEmoticons:[YMEmoticon] = {
        return self.getEmoticons("lxh/")
    }()
    
    
    
    
    //MARK:==========一维数组=============
    //通过plist文件加载
    func getEmoticons(name:String) ->[YMEmoticon]{
        let path = enmoticonBundle.pathForResource("\(name)info.plist", ofType: nil)!
        //plist文件转数组
        let array = NSArray(contentsOfFile:path)!
        
        var tempArr:[YMEmoticon] = [YMEmoticon]()
        
        for value in array {
            let emoticon:YMEmoticon = YMEmoticon(dic: value as! [String : AnyObject])
            
            emoticon.path = "\(name)\(emoticon.png ?? "")"
//            print(emoticon.path)
            tempArr.append(emoticon)

        }
        return tempArr
    }
    
    
    
    
    //MARK:==========二维数组=============
    //通过切割一维数组,创建而位置数组
    func getEmoticonGroup(emoticons:[YMEmoticon]) ->[[YMEmoticon]] {
        var count = 0
        //判断是否区域大于0,大于0多一行
        if emoticons.count%YMEmoticonMaxCount > 0 {
            count = emoticons.count/YMEmoticonMaxCount + 1
        }
        
        count = emoticons.count/YMEmoticonMaxCount
        
        var tempArr :[[YMEmoticon]] = [[YMEmoticon]]()
        
        for i in 0..<count {
            //位置
            let location = YMEmoticonMaxCount * i
            
            //长度
            var length = YMEmoticonMaxCount
            
            if location + length > emoticons.count{
                length = emoticons.count - location
            }
            
            //range<截取>
            let range = NSRange(location:location ,length: length)
            let array = (emoticons as NSArray).subarrayWithRange(range) as! [YMEmoticon]
            
            tempArr.append(array)
        }
        return tempArr
    }
    
    
    
    
    
    
    //MARK:==========三维数组=============
    
    lazy var allEmoticons:[[[YMEmoticon]]] = {
    
        return [
            self.getEmoticonGroup(self.defaultEmoticons),
            self.getEmoticonGroup(self.emojiEmoticons),
            self.getEmoticonGroup(self.lxhEmoticons)
        
        ]
        
    }()
    
    
    
   //传入图片的名称得到对应的模型
    
    func searchEmticons(chs:String?) ->YMEmoticon?{
        
        for emoticon in defaultEmoticons {
            //判断是否相同
            
            if emoticon.chs == chs{
                return emoticon
            }
        }
        
        //遍历小浪花表情组
        for emoticon in lxhEmoticons {
            if emoticon.chs == chs {
                return emoticon
            }
        }
        return nil
    }
    
    
    
    
}
